package com.lpj.base.id.generator.service.impl.bean;

public class IdMeta {

    private byte machineBits;

    private byte seqBits;

    private byte timeBits;

    private byte typeBits;

    private byte versionBits;

    //时间位数的精确度 0 毫秒级， 1 秒级
    private byte extTimeType;

    public IdMeta(byte machineBits, byte seqBits, byte timeBits, byte typeBits, byte versionBits, byte extTimeType) {
        super();

        this.machineBits = machineBits;
        this.seqBits = seqBits;
        this.timeBits = timeBits;
        this.typeBits = typeBits;
        this.versionBits = versionBits;
        this.extTimeType = extTimeType;
    }

    public byte getMachineBits() {
        return machineBits;
    }

    public void setMachineBits(byte machineBits) {
        this.machineBits = machineBits;
    }

    public long getMachineBitsMask() {
        return -1L ^ -1L << machineBits;
    }

    public byte getSeqBits() {
        return seqBits;
    }

    public void setSeqBits(byte seqBits) {
        this.seqBits = seqBits;
    }

    public long getSeqBitsStartPos() {
        return machineBits;
    }

    public long getSeqBitsMask() {
        return -1L ^ -1L << seqBits;
    }

    public byte getTimeBits() {
        return timeBits;
    }

    public void setTimeBits(byte timeBits) {
        this.timeBits = timeBits;
    }

    public long getTimeBitsStartPos() {
        return machineBits + seqBits;
    }

    public long getTimeBitsMask() {
        return -1L ^ -1L << timeBits;
    }

    public byte getTypeBits() {
        return typeBits;
    }

    public void setTypeBits(byte typeBits) {
        this.typeBits = typeBits;
    }

    public long getTypeBitsStartPos() {
        return machineBits + seqBits + timeBits;
    }

    public long getTypeBitsMask() {
        return -1L ^ -1L << typeBits;
    }

    public byte getVersionBits() {
        return versionBits;
    }

    public void setVersionBits(byte versionBits) {
        this.versionBits = versionBits;
    }

    public long getVersionBitsStartPos() {
        return machineBits + seqBits + timeBits + typeBits;
    }

    public long getVersionBitsMask() {
        return -1L ^ -1L << versionBits;
    }

    public long getMaxMachineId() {
        return -1L ^ -1L << machineBits;
    }

    public byte getExtTimeType() {
        return extTimeType;
    }

    public void setExtTimeType(byte extTimeType) {
        this.extTimeType = extTimeType;
    }

    public long getTimestamp(long timestamp) {
        if (extTimeType == 0) {
            return timestamp / 1000;
        } else if (extTimeType == 1) {
            return timestamp;
        }
        return timestamp / 1000;
    }
}
